package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.basic.MobileMessageTemplate;
import cc.yugu.yhdw.common.tool.memcached.MemCachedConfig;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 短信模板切面类
 * 
 * @Date:Created in 2018/7/3 17:24
 * @Author:
 * @Modified By: zjn 2018/7/3 17:24
 */ 
@Component
@Aspect
public class MobileMessageTemplateServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

//    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.MobileMessageTemplateService.find(..))")
//    private void find() {}
//
//    @Around("find()")
//    public Object find(ProceedingJoinPoint joinPoint) throws Throwable {
//        Integer agentId = (Integer) joinPoint.getArgs()[0];
//        Integer id = (Integer) joinPoint.getArgs()[1];
//        String key = CacheKey.key(CacheKey.K_ID_V_MOBILE_MESSAGE_TEMPLATE, agentId, id);
//        MobileMessageTemplate mobileMessageTemplate = (MobileMessageTemplate)memCachedClient.get(key);
//        if(mobileMessageTemplate != null) {
//            return mobileMessageTemplate;
//        }
//
//        Object result = joinPoint.proceed();
//        if(result != null) {
//            memCachedClient.set(key, result, MemCachedConfig.CACHE_ONE_WEEK);
//        }
//        return result;
//    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.MobileMessageTemplateService.update(..))")
    private void update() {}
    /**
    * 更新
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/3  17:24
    */ 
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        MobileMessageTemplate mobileMessageTemplate = (MobileMessageTemplate) joinPoint.getArgs()[0];
        Object result = joinPoint.proceed();

        String key = CacheKey.key(CacheKey.K_ID_V_MOBILE_MESSAGE_TEMPLATE, mobileMessageTemplate.getAgentId(), mobileMessageTemplate.getId());
        memCachedClient.delete(key);
        return result;
    }
}
